refactor: migrate metal_vlan to equinix-sdk-go#782
Conversation
aec417d to
e6bf099
Compare
|
I left the device linter errors in place here because they are already fixed in #707 which is explicitly focused on the device code. |
|
#707 has been merged - rebase away! |
e6bf099 to
39f4b94
Compare
39f4b94 to
cf055c4
Compare
cf055c4 to
3a4e165
Compare
| name: "ComplexMatch", | ||
| args: args{ | ||
| vlans: []packngo.VirtualNetwork{{VXLAN: 987, FacilityCode: "fac", MetroCode: "skip"}, {VXLAN: 123, FacilityCode: "fac", MetroCode: "met"}, {VXLAN: 456, FacilityCode: "fac", MetroCode: "nope"}}, | ||
| vlans: []metalv1.VirtualNetwork{{Vxlan: metalv1.PtrInt32(987), AdditionalProperties: map[string]interface{}{"facility_code": "fac"}, MetroCode: metalv1.PtrString("skip")}, {Vxlan: metalv1.PtrInt32(123), AdditionalProperties: map[string]interface{}{"facility_code": "fac"}, MetroCode: metalv1.PtrString("met")}, {Vxlan: metalv1.PtrInt32(456), AdditionalProperties: map[string]interface{}{"facility_code": "fac"}, MetroCode: metalv1.PtrString("nope")}}, |
There was a problem hiding this comment.
nit: breaking this up with newlines may help readers without line-wrapping views
| if vlan.Description != "" { | ||
| m.Description = types.StringValue(vlan.Description) | ||
| if vlan.GetDescription() != "" { | ||
| m.Description = types.StringValue(vlan.GetDescription()) |
There was a problem hiding this comment.
Empty or not, when would we not want the model description to match the upstream description?
There was a problem hiding this comment.
There may be other ways to address the bug fixed by that previous PR; I forget what else, if anything, I tried at that time, but I think I was aiming to maintain backwards compatibility. The effect of this if block is that, if the description is null or "" in the response, it is null in the model. I don't recall if the API meaningfully differentiates between a null VLAN description and an empty VLAN description.
|
|
||
| if vlan.Description != "" { | ||
| m.Description = types.StringValue(vlan.Description) | ||
| if vlan.GetDescription() != "" { |
There was a problem hiding this comment.
same comment as above, why wouldn't we set description unconditionally?
This was broken out of #782 since it involves a widely-used helper function and will impact more than just the Metal VLAN resource. Previously, the `IgnoreHttpResponseErrors` helper function accepted as arguments a number of other, existing helper functions that are meant to evaluate whether a particular API response has a certain HTTP status. Here's [an example helper function for checking if the response is a 403](https://github.com/equinix/terraform-provider-equinix/blob/b1ed71b389705513308c1e47e7ac6ff0c46891cd/internal/errors/errors.go#L119-L130): ```go func HttpForbidden(resp *http.Response, err error) bool { if resp != nil && (resp.StatusCode != http.StatusForbidden) { return false } switch err := err.(type) { case *ErrorResponse, *packngo.ErrorResponse: return IsForbidden(err) } return false } ``` This function immediately returns false if the response status code _is not_ 403; otherwise it tries to convert the error argument to an `ErrorResponse` or a `packngo.ErrorResponse` and then use a different helper function to determine if that error represents a 403. This assumes that the error object contains a copy of the response, which is only certain to be the case for code that uses `packngo`. In practice we almost never did the necessary conversion to `ErrorResponse` for non-`packngo` resources and data sources, so in many cases we were probably not successfully ignoring the desired response codes. This refactors `IgnoreHttpResponseErrors` to inspect HTTP status codes directly in the response instead of relying on helper functions for specific custom error types. This also removes the `FriendlyErrorForMetalGo` function which only existed in order to convert a separate HTTP response and error into an `ErrorResponse`, which is no longer necessary. Some usage of `FriendlyError` is also removed in cases where that function had no or minimal effect.
ad9fdca to
51ebfb3
Compare
51ebfb3 to
df4307e
Compare
df4307e to
d60efc0
Compare
d60efc0 to
228c73f
Compare
f945596 to
83efc97
Compare
83efc97 to
5b5800d
Compare
remove non-functional code found by linter
5b5800d to
c6256f4
Compare
There was a problem hiding this comment.
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 suggestion.
Comments skipped due to low confidence (1)
internal/resources/metal/vlan/datasource.go:110
- The type assertion for 'facility_code' should be checked to avoid potential runtime panic. Use a safe type assertion.
facility_code, ok := v.AdditionalProperties["facility_code"].(string)
| client := acceptance.TestAccProvider.Meta().(*config.Config).NewMetalClientForTesting() | ||
|
|
||
| foundVlan, _, err := client.ProjectVirtualNetworks.Get(rs.Primary.ID, nil) | ||
| foundVlan, _, err := client.VLANsApi.GetVirtualNetwork(context.Background(), rs.Primary.ID).Execute() |
There was a problem hiding this comment.
Using context.Background() directly in tests is not recommended. Consider using a context that can be controlled or canceled.
| foundVlan, _, err := client.VLANsApi.GetVirtualNetwork(context.Background(), rs.Primary.ID).Execute() | |
| foundVlan, _, err := client.VLANsApi.GetVirtualNetwork(context.WithTimeout(context.Background(), time.Second*10), rs.Primary.ID).Execute() |
This was broken out of equinix#782 since it involves a widely-used helper function and will impact more than just the Metal VLAN resource. Previously, the `IgnoreHttpResponseErrors` helper function accepted as arguments a number of other, existing helper functions that are meant to evaluate whether a particular API response has a certain HTTP status. Here's [an example helper function for checking if the response is a 403](https://github.com/equinix/terraform-provider-equinix/blob/b1ed71b389705513308c1e47e7ac6ff0c46891cd/internal/errors/errors.go#L119-L130): ```go func HttpForbidden(resp *http.Response, err error) bool { if resp != nil && (resp.StatusCode != http.StatusForbidden) { return false } switch err := err.(type) { case *ErrorResponse, *packngo.ErrorResponse: return IsForbidden(err) } return false } ``` This function immediately returns false if the response status code _is not_ 403; otherwise it tries to convert the error argument to an `ErrorResponse` or a `packngo.ErrorResponse` and then use a different helper function to determine if that error represents a 403. This assumes that the error object contains a copy of the response, which is only certain to be the case for code that uses `packngo`. In practice we almost never did the necessary conversion to `ErrorResponse` for non-`packngo` resources and data sources, so in many cases we were probably not successfully ignoring the desired response codes. This refactors `IgnoreHttpResponseErrors` to inspect HTTP status codes directly in the response instead of relying on helper functions for specific custom error types. This also removes the `FriendlyErrorForMetalGo` function which only existed in order to convert a separate HTTP response and error into an `ErrorResponse`, which is no longer necessary. Some usage of `FriendlyError` is also removed in cases where that function had no or minimal effect.
No description provided.